const express = require('express')
const app = express()
const port = 8080

app.use(express.json())
app.use((req,res,next) =>{
    res.header('Access-Control-Allow-Origin','*')
    res.header('Access-Control-Allow-Methods','GET, POST, OPTIONS, PUT, PATCH, DELETE')
    res.header('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept')
    next()
})

//注册表
const users = [
    {id:1,username:'admin',password:'123456'}
]

app.get('/',(_req,res) =>{
    res.send('欢迎您!')
})

app.post('/login',(req,res) =>{
    const {username,password} = req.body
    console.log(username,password)
    const user = users.find(user => user.username === username && user.password === password)

    if(user) {
        const token = Math.floor(Math.random()*1000)
        res.status(200).send({token})
    }else{
        res.status(401).send('账号或密码错误')
    }
})

app.listen(port,() =>{
    console.log(`http://localhost:${port}`)
})